package com.hospital.service.util;

import com.hospital.entity.HospitalizationRecord;
import com.hospital.entity.Patient;
import com.hospital.enumeration.BusinessExceptionEnum;
import com.hospital.exception.BusinessException;
import com.hospital.mapper.HospitalizationRecordMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class PatientCheckUtil {
    @Autowired
    private HospitalizationRecordMapper hospitalizationRecordMapper;

    public void checkPatientExists(Patient patient) {
        // 如果病人不存在，则抛出异常
        if (patient == null) {
            throw new BusinessException(BusinessExceptionEnum.PATIENT_NOT_EXIST);
        }
    }

    public void checkPatient(Patient patient) {
        checkPatientExists(patient);

        // 如果已出院
        HospitalizationRecord hospitalizationRecord = hospitalizationRecordMapper.selectById(patient.getPatientId());
        if (hospitalizationRecord.getDischargeTime() != null) {
            throw new BusinessException(BusinessExceptionEnum.PATIENT_ALREADY_DISCHARGED);
        }
    }
}
